<?php

/**
 * BKTiny Framework
 * Core_Template class
 * 
 * LICENSE:
 * BKTiny Framework duoc xay dung va phat trien boi nhom BKloud - dai hoc bach khoa Ha Noi 
 * Ban duoc phep su dung framework nay cho muc dich hoc tap , nghien cuu va cac ung dung phi thuong mai
 * 
 * @copyright Copyright (c) 2011 BKloud - HPCC.
 * @version 0.1
 */
class Core_Template
{
    /**
     * Registry to use
     *
     * @var Core_Registry
     */
    private $_registry;

    /**
     * Mang luu tru cap ten bien - gia tri : cac gia tri can truyen cho template de hien thi tren view
     *
     * @var array
     */
    private $_vars = array();

    /**
     * Su dung layout (true/false)
     *
     * @var bool
     */
    private $_usingLayout;

    /**
     * Ten cua layout duoc su dung
     *
     * @var string
     */
    private $_nameLayout;

    /**
     * Base Url
     *
     * @var string
     */
    private $_baseUrl;
    /**
     * Constructor
     *
     * Khoi tao Template.
     * Lay doi tuong duy nhat $registry cua Core_Registry.
     * Gan gia tri mac dinh cho _layout la 'layout'
     * 
     * @return void
     */
    public function __construct()
    {
        $this->_registry    = Core_Registry::singleton();
        $this->_usingLayout = $this->_registry['config']['layout']['usingLayout'];
        $this->_nameLayout  = $this->_registry['config']['layout']['nameLayout'];
        $this->_baseUrl     = '';
        $baseUrl            = $this->_registry['http']->getBaseUrl();
        if($baseUrl != '')
        $this->_baseUrl     = '/' .$baseUrl ;
    }


    public function set($varname, $value)
    {
        $this->_vars[$varname] = $value;
    }

    public function remove($varname)
    {
        unset($this->_vars[$varname]);
    }

    public function get($varname)
    {
        if (isset($this->_vars[$varname]))
            return $this->_vars[$varname];
        return null;
    }

    public function __set($varname, $value)
    {
        $this->set($varname, $value);
    }

    public function __get($varname)
    {
        return $this->get($varname);
    }

    public function layout($usingLayout, $nameLayout = null)
    {
        if ($usingLayout)
        {
            $this->_usingLayout = true;
            if (isset($nameLayout))
                $this->_nameLayout = $nameLayout;
        } else
            $this->_usingLayout = false;
    }

    /**
     * 
     * include file widget
     * @param  string  $fileName : ten cua widget can su dung
     *      
     * @return void
     */
    public function loadWidget($fileName)
    {
        $http = $this->_registry['http'];
        $module = $http->getModule();
        $fileWidget = APPLICATION_PATH . '/modules/' . $module . '/views/widgets/' . $fileName .
            '.php';
        if (is_readable($fileWidget))
            include ($fileWidget);
        else
            echo "Widget $fileName Not Found !";
    }

    /**
     * 
     * include file view duoc luu trong /views/scripts/$controller/$action.php
     *      
     * @return void
     */
    public function loadContent()
    {
        $http   = $this->_registry['http'];
        $module = $http->getModule();
        $controller = $http->getController();
        $action     = $http->getAction();

        $controllerView = substr($controller, 0, strrpos($controller, "Controller"));
        $actionView     = substr($action, 0, strrpos($action, "Action"));

        $controllerView = strtolower($controllerView);
        $actionView     = strtolower($actionView);

        $fileView = APPLICATION_PATH . '/modules/' . $module . '/views/scripts/' . $controllerView . '/' . $actionView . '.php';
        if (is_readable($fileView))
            include ($fileView);
        else
            echo "View $controllerView/$actionView Not Found !";
    }

    /**
     * 
     * Hien thi layout
     *      
     * @return void
     */
    public function show()
    {
        if ($this->_usingLayout == false)
        {
            $this->loadContent();
        } else
        {
            $http = $this->_registry['http'];
            $module = $http->getModule();

            $fileLayout = APPLICATION_PATH . '/modules/' . $module . '/views/layouts/' . $this->
                _nameLayout . '.php';
            if (is_readable($fileLayout))
                include ($fileLayout);
            else
                echo "Layout Not Found !";
        }

    }

}
